/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unitn.science.matr155863_148346_145963.bccstore.servlet;

import it.unitn.science.matr155863_148346_145963.bccstore.DBManager;
import it.unitn.science.matr155863_148346_145963.bccstore.items.Category;
import it.unitn.science.matr155863_148346_145963.bccstore.items.Product;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author berto
 */
public class SearchFormSRV extends HttpServlet {
    private DBManager db;
    private String srcCategory;
    private static Logger log = Logger.getLogger(SearchFormSRV.class);
    
    @Override
    public void init() throws UnavailableException{
            db=(DBManager)super.getServletContext().getAttribute("db_manager");
            
            ArrayList<Category> listCat=db.getCategories();
            srcCategory="<select name=\""+Product.CATEGORY+"\">";
            for(Category c:listCat){
                srcCategory=srcCategory+"<option value=\""+c.getId()+"\" >"+c.getName()+"</option>";
            }
            srcCategory=srcCategory+"</select>";
    }
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String priceStr=request.getParameter("price");
            if(priceStr==null){
                priceStr="";
            }
            String seller=request.getParameter("seller");
            if(seller==null){
                seller="";
            }
            out.println("<h3>Select the parameter of your search</h3>"
                + "<form name=\"searchProduct\" method=\"post\""
                + "action=\""+response.encodeURL(request.getContextPath()+"/contents/public/SearchProducts")+"\" >"
                + "<table>"
                    + "<tr>"
                        + "<td><label>Category:</label></td>"
                        + "<td>"
                            + "<label>Price less then:</label>"
                        + "</td>"
                        + "<td><label>Seller:</label></td>"
                        + "<td><input type=\"reset\" name=\"reset\" value=\"Reset\" class=\"button\"/></td>"
                    + "</tr>"
                    + "<tr>"
                        + "<td>"
                            + srcCategory
                        + "</td>"
                        + "<td><input type=\"text\" name=\"price\" value=\""+priceStr+"\"/><label>euro</label></td>"
                        + "<td><input type=\"text\" name=\"seller\" value=\""+seller+"\"/></td>"
                        + "<td><input type=\"submit\" name=\"Search\" class=\"button\" value=\"Search\"/></td>"
                    + "</tr>"
                + "</table>"
                + "</form>");
        }catch(Exception ex){
            log.error(ex);
            RequestDispatcher dispatcher=request.getRequestDispatcher("/contents/public/errorPage.html");
            dispatcher.include(request, response);
        } finally {            
            //out.close();
        }
    }
    
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
